﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace Xfs
{
	[XfsObjectSystem]
	public class XfsStartConfigAwakeComponentAwakeSystem : XfsAwakeSystem<XfsStartConfigComponent>
	{
		public override void Awake(XfsStartConfigComponent self)
		{
			self.Awake();
		}
	}

	public class XfsStartConfigComponent : XfsComponent
	{
		public static XfsStartConfigComponent Instance { get; private set; }

		private Dictionary<long, XfsStartConfig> configDict = new Dictionary<long, XfsStartConfig>();

		private Dictionary<long, IPEndPoint> innerAddressDict = new Dictionary<long, IPEndPoint>();
		private Dictionary<long, IPEndPoint> outerAddressDict = new Dictionary<long, IPEndPoint>();

		public XfsStartConfig? StartConfig { get; private set; }
		public XfsLocationConfig? XfsLocationConfig { get; private set; }
		public XfsInnerConfig? XfsInnerConfig { get; private set; }
		public IPEndPoint? IpEendPoint{ get; set; }

		public List<XfsStartConfig> StartConfigs { get; private set; } = new List<XfsStartConfig>();

		public XfsStartConfig? DBConfig { get; private set; }

		public XfsStartConfig? RealmConfig { get; private set; }

		public XfsStartConfig? LocationConfig { get; private set; }

		public List<XfsStartConfig>? MapConfigs { get; private set; }

		public List<XfsStartConfig>? GateConfigs { get; private set; }


		public void Awake()
        {
			Instance = this;

			this.IpEendPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4002) as IPEndPoint;
			this.StartConfig = new XfsStartConfig() { Adress = "127.0.0.1", Port = 4002, MaxLiningCount = 10 };
			this.StartConfig.SenceType = XfsGame.XfsSence.Type;

			this.configDict.Add((int)this.StartConfig.SenceType, this.StartConfig);
			this.StartConfigs.Add(this.StartConfig);
		}

		public override void Dispose()
		{
			if (this.IsDisposed)
			{
				return;
			}
			base.Dispose();

			Instance = null;
		}

		public XfsStartConfig Get(int id)
		{
			try
			{
				return this.configDict[id];
			}
			catch (Exception e)
			{
				throw new Exception($"not found startconfig: {id}", e);
			}
		}

		public IPEndPoint GetInnerAddress(long id)
		{
            if (this.innerAddressDict[id] != null)
            {
				return this.innerAddressDict[id];
			}
			this.innerAddressDict.Add(id, this.IpEendPoint);
			return this.IpEendPoint;
		}
		public void AddInnerAddress(long id , IPEndPoint iPEndPoint)
		{
			if(innerAddressDict.TryGetValue(id,out IPEndPoint iPEndPoint1))
            {
				this.innerAddressDict.Remove(id);
            }
			this.innerAddressDict.Add(id, iPEndPoint);
		}
		public void AddInnerAddress(long id)
		{
			if (innerAddressDict.TryGetValue(id, out IPEndPoint iPEndPoint1))
			{
				this.innerAddressDict.Remove(id);
			}
			this.innerAddressDict.Add(id, this.IpEendPoint);
		}

		public IPEndPoint GetOutAddress(int id)
		{
			if (this.outerAddressDict[id] != null)
			{
				return this.outerAddressDict[id];
			}
			this.outerAddressDict.Add(id, this.IpEendPoint);
			return this.IpEendPoint;
		}
		public void AddOuterAddress(int id)
		{
			if (outerAddressDict.TryGetValue(id, out IPEndPoint iPEndPoint1))
			{
				this.outerAddressDict.Remove(id);
			}
			this.outerAddressDict.Add(id, this.IpEendPoint);
		}
		public void AddOuterAddress(long id, IPEndPoint iPEndPoint)
		{
			if (outerAddressDict.TryGetValue(id, out IPEndPoint iPEndPoint1))
			{
				this.outerAddressDict.Remove(id);
			}
			this.outerAddressDict.Add(id, iPEndPoint);
		}
		
		public XfsStartConfig[] GetAll()
		{
			return this.configDict.Values.ToArray();
		}

		public int Count
		{
			get
			{
				return this.configDict.Count;
			}
		}


	}
}
